package com.weipai.education.dao.impl;

import com.weipai.education.dao.PurchaseDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.mapper.ProductGoodsMapper;
import com.weipai.education.mapper.PurchaseDetailMapper;
import com.weipai.education.mapper.PurchaseMapper;
import com.weipai.education.model.ProductGoods;
import com.weipai.education.model.Purchase;
import com.weipai.education.model.PurchaseDetail;
import com.weipai.education.model.api.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/13 09:59
 */
@Repository
public class PurchaseDaoImpl implements PurchaseDao {

    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private PurchaseDetailMapper purchaseDetailMapper;
    @Autowired
    private ProductGoodsMapper productGoodsMapper;


    @Override
    public int insert(Purchase purchase) {
        return purchaseMapper.insert(purchase);
    }

    @Override
    public List<Map<String, Object>> list(Integer start, Integer size, String startTime, String endTime, 
                                          String key, String userId, String type, String status) {
        return purchaseMapper.list(start, size, startTime, endTime, key, userId, type, status);
    }

    @Override
    public Integer listAmount(String startTime, String endTime, String key, String userId, String type, String status) {
        return purchaseMapper.listAmount(startTime, endTime, key, userId, type, status);
    }

    @Override
    public Purchase selectByPrimaryKey(String id) {
        return purchaseMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public List<PurchaseDetail> getGoodsBarCodeByPurchaseNumber(String purchaseNumber) {
        return purchaseDetailMapper.getGoodsBarCodeByPurchaseNumber(purchaseNumber);
    }

    @Override
    public Purchase getPurchaseByPurchaseNumber(String purchaseNumber) {
        return purchaseMapper.getPurchaseByPurchaseNumber(purchaseNumber);
    }

    @Override
    public int updateByPrimaryKeySelective(Purchase purchase) {
        return purchaseMapper.updateByPrimaryKeySelective(purchase);
    }

    @Override
    public Map<String, Object> getPurchaseInfo(String purchaseNumber) {
        return purchaseMapper.getPurchaseInfo(purchaseNumber);
    }

    @Override
    public List<Map<String, Object>> getWeekPurchase(Integer start, Integer size, String userId) {
        return purchaseMapper.getWeekPurchase(start, size, userId);
    }

    @Override
    public List<Map<String, Object>> getMonthPurchase(Integer start, Integer size, String userId) {
        return purchaseMapper.getMonthPurchase(start, size, userId);
    }

    @Override
    public List<Map<String, Object>> getDayPurchase(String userId) {
        return purchaseMapper.getDayPurchase(userId);
    }

    @Override
    public Integer getWeekPurchaseAmount(String userId) {
        return purchaseMapper.getWeekPurchaseAmount(userId);
    }

    @Override
    public Integer getMonthPurchaseAmount(String userId) {
        return purchaseMapper.getMonthPurchaseAmount(userId);
    }

    @Override
    public Response verifyGoodsBarCodes(String productId, String goodsBarCodes) {
        Response response = new Response();
        String[] fields = goodsBarCodes.split("\n");
        for(String goodsBarCode : fields){

            //查看总部的货号中是否有此商品编码
            ProductGoods productGoods = productGoodsMapper.verifyGoodsBarCodes(productId, goodsBarCode);

            if(null == productGoods){
                response.setCode(RetCode.NOT_DATA);
                response.put("goodsBarCode", "未找到["+goodsBarCode+"]商品");
                return response;
            }
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public void insertProductDetail(PurchaseDetail purchaseDetail) {
        purchaseDetailMapper.insert(purchaseDetail);
    }

    @Override
    public Map<String,Object> getPurchaseDetail(String purchaseNumber, String goodsBarCode) {
        return purchaseDetailMapper.getPurchaseDetail(purchaseNumber, goodsBarCode);
    }

    @Override
    public Integer getDayPurchaseAmount(String userId) {
        return purchaseMapper.getDayPurchaseAmount(userId);
    }

    @Override
    public Map<String, Object> getAllAmount(String userId) {
        return purchaseMapper.getAllAmount(userId);
    }

    @Override
    public List<Map<String, Object>> cityLevelList(Integer start, Integer size, String startTime, String endTime, String key, List<Integer> idList) {
        return purchaseMapper.cityLevelList(start, size, startTime, endTime, key, idList);
    }

    @Override
    public Integer cityLevelListAmount(String startTime, String endTime, String key, List<Integer> idList) {
        return purchaseMapper.cityLevelListAmount(startTime, endTime, key, idList);
    }

    @Override
    public Response verifyGoodsBarCodesIsUse(String productId, String goodsBarCodes) {
        Response response = new Response();
        String[] fields = goodsBarCodes.split("\n");
        for(String goodsBarCode : fields){

            //验证此条形码是否使用
            ProductGoods productGoods = productGoodsMapper.verifyGoodsBarCodes(productId, goodsBarCode);

            if(productGoods.getIsUse() == 1){
                response.setCode(RetCode.NOT_DATA);
                response.put("goodsBarCode", "条形码["+goodsBarCode+"]已使用，请勿重复入库");
                return response;
            }
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Map<String,Object> searchOrderByOrderNumber(String OrderNumber,Integer userId) {
        return purchaseMapper.searchOrderByOrderNumber(OrderNumber,userId);
    }
    @Override
    public  Integer selectBarcodeByOrderNumber(String BarCode,String OrderNumber){
        return purchaseDetailMapper.selectBarcodeByOrderNumber(BarCode,OrderNumber);
    }

    @Override
    public List<Map<String, Object>> getAllGoodsBar(String orderNumber) {
        return purchaseDetailMapper.getAllGoodsBar(orderNumber);
    }
}
